var TigerManageAd = {
	account : null,		
	load : function () {
		var qs =  new QueryString();
		TigerManageAd.account = qs.get("customerAccount");
		if (!TigerManageAd.account) {
			TigerManageAd.account = getUser().Account;
		}
		 
		Ext.QuickTips.init();
		this.eventRegister();
 
		TigerManageAd.createGrid(); //建立 Ext table
  
		waitShow()
		Ext.Ajax.request ({
			url : "/ad/sys/com/control/AdService.php?action=menagerAd&account="+TigerManageAd.account,
			callback : function (op, sc, resp) {
			  var obj = null;
			  try{
				  obj =  resp.responseText.toJSON();	
			  } catch (e) {
					alert("資料錯誤:"+e);
					waitHide();
					return;
			  }
				var str = "";
				
				if ( obj.notify.length ==0 ) {
					str = '<li><font color="red" size=3>「您尚未刊登任何廣告，<a title="立即刊登廣告" href="/ad/html/frontoffice/tigerStep1.php">請立即使用</a>。」</font></li>';
				} 
				
				var notify = new Ext.XTemplate(
		            str,	
					'<tpl for=".">',
		               '<li>{#}.<span class="date">{CreateTime:this.fn}</span><span class="announce">{Content}</span></li>',
		            '</tpl>',
		            {
		            	fn : function (time) {
		            		return time.toDateString()
		            	}
		            }
		        );
		    
		        var pay = new Ext.XTemplate(
		        	'<tpl for=".">',
		               '<li>{#}.<span class="date">{createtime:this.fn}</span>	<span class="currency">{pay}(元)</span></li>',
		            '</tpl>',
		            {
			            fn : function (time) {
			            	return time.toDateString("mm月dd日");
			            }
		            }
		        );
		        notify.overwrite("notify", obj.notify);
				pay.overwrite("pay", obj.pay);
				
				waitHide();
		       
			}
		})  
		
	},
	 
	eventRegister : function () {
	    Ext.get("btnQuery").on('click',this.query);  //確認,新增使用者	
	}, 
	
	query : function () {
		var whereDate = Ext.getDom("whereDate").value;
		var whereStatus = Ext.getDom("whereStatus").value;
		
		
		TigerManageAd.store.reload({
			params:{
				start:0,
				limit:10,
				whereDate : whereDate,
				whereStatus : whereStatus
			}
		});  
		
	},
	
	createGrid : function () {
	 	 TigerManageAd.store = new Ext.data.JsonStore ({
	 	 	url:"/ad/sys/com/control/AdService.php?action=queryAdByCreatetimeAndStatus&account="+TigerManageAd.account,
			root : "row", 
			remoteSort: false,
			totalProperty: 'count',
			fields : ["AdName", "Status", "AdStartDate", "AdEndDate","Budget","Overage","Outlay"]
		});
		
		 
		TigerManageAd.grid =new Ext.grid.GridPanel({
			renderTo : "grid",
			store :  TigerManageAd.store,
			height: 340,
		    //autoWidth: true,
			loadMask : true,
			viewConfig : {
				forceFit : true 
			},
			columns : [ 
				new Ext.grid.RowNumberer(),
				{header : "活動" ,dataIndex : "AdName", width :100,sortable : true },
				{header : "狀態", dataIndex : "Status",width :40, sortable : true, 
					renderer: function (value, p , record) {
						switch (value) {
							case "0": return "審理中";
							case "1": return "刊登中";
							case "2": return "已結束";
							case "3": return "暫停";
							case "4": return "刪除";
							case "5": return "未通過";
							case "6": return "需修改";
						}
					} 
					 
				},
				{header : "開始時間", dataIndex : "AdStartDate",width :50,sortable : true},
				{header : "結束時間", dataIndex : "AdEndDate",width :50 , sortable : true},
				{header : "預算", dataIndex : "Budget" , width : 30, sortable : true},
				{header : "餘額", dataIndex : "Overage",width :30, sortable : true,
					renderer: function (value, p , record) {
					    return record.data.Budget - record.data.Outlay;
					} 
				},
				{header : "已支出費用", dataIndex : "Outlay" , width : 30, sortable : true}
			],
			
			bbar : new Ext.PagingToolbar({
				pageSize :10,
				store : TigerManageAd.store,
				displayInfo : true,
				displayMsg : "顯示第 {0} 筆到 {1} 筆紀錄,共 {2} 筆紀錄",
				emptyMsg : "沒有紀錄" 
			})
		});	
		
		TigerManageAd.store.load({
			params:{
				start:0,
				limit:10
			}
		});    
	}
	 
};

Ext.onReady(function (){
	TigerManageAd.load();
});


